package com.cn.cxq.controller;

import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.cn.cxq.bean.DBTest;
import com.cn.cxq.bean.Default;
import com.cn.cxq.util.DynamicDataSource;
import com.cn.cxq.util.SQLUtil;

@Controller
@RequestMapping("query")
public class SqlQueryController {
	@Autowired
	Default def;
	@Autowired
	DBTest dbt;
	
	@RequestMapping
	public String query(Model model, HttpServletRequest request) throws SQLException{
		
		DynamicDataSource DDataSource = SQLUtil.getDynamicDataSource(request);
		Map<Object, Object> targetDataSources = DDataSource.getTargetDataSources();
		int i = 0;
		for (Map.Entry entry : targetDataSources.entrySet()) {
			Object lookupKey = entry.getKey();
			DataSource dataSource = DDataSource.resolveSpecifiedDataSource(entry.getValue());
			String GName = dbt.dbtest(dataSource.getConnection());
			
			model.addAttribute("meg" + i, GName);
			i++;
		}
		return "/login";
		
	}
}
